#ifndef __USER_TCS3410_DRIVER_H
#define __USER_TCS3410_DRIVER_H

#include "main.h"

/*
    TCS3410 device Addresses
    TCS34103 & TCS34103M 7bits addrs = 0x39
    TCS34109             7bits addrs = 0x49
*/
#define TCS3410_DEVICE_ADDR                         0x39 << 1
/*
    TCS3410 Reg Addresses
*/
#define TCS3410_DISABLE_REG                         0x40
#define TCS3410_ENABLE_REG                          0x80
#define TCS3410_MEAS_MODE0_REG                      0x81
#define TCS3410_MEAS_MODE1_REG                      0x82
#define TCS3410_MEAS_TIME0_REG                      0x83
#define TCS3410_MEAS_TIME1_REG                      0x84
#define TCS3410_MEAS_ALS_NR_SAMPLES0_REG            0x85
#define TCS3410_MEAS_ALS_NR_SAMPLES1_REG            0x86
#define TCS3410_MEAS_FD_NR_SAMPLES0_REG             0x87
#define TCS3410_MEAS_FD_NR_SAMPLES1_REG             0x88
#define TCS3410_WTIME_REG                           0x89
#define TCS3410_AILT0_REG                           0x8A
#define TCS3410_AILT1_REG                           0x8B
#define TCS3410_AILT2_REG                           0x8C
#define TCS3410_AIHT0_REG                           0x8D
#define TCS3410_AIHT1_REG                           0x8E
#define TCS3410_AIHT2_REG                           0x8F
#define TCS3410_AUX_ID_REG                          0x90
#define TCS3410_REV_ID_REG                          0x91
#define TCS3410_ID_REG                              0x92
#define TCS3410_STATUS_REG                          0x93
#define TCS3410_ALS_STATUS_REG                      0x94
#define TCS3410_ALS_DATA0_L_REG                     0x95
#define TCS3410_ALS_DATA0_H_REG                     0x96
#define TCS3410_ALS_DATA1_L_REG                     0x97
#define TCS3410_ALS_DATA1_H_REG                     0x98
#define TCS3410_ALS_DATA2_L_REG                     0x99
#define TCS3410_ALS_DATA2_H_REG                     0x9A
#define TCS3410_ALS_STATUS2_REG                     0x9B
#define TCS3410_ALS_STATUS3_REG                     0x9C
#define TCS3410_STATUS2_REG                         0x9D
#define TCS3410_STATUS3_REG                         0x9E
#define TCS3410_STATUS4_REG                         0x9F
#define TCS3410_STATUS5_REG                         0xA0
#define TCS3410_CFG0_REG                            0xA1
#define TCS3410_CFG1_REG                            0xA2
#define TCS3410_CFG2_REG                            0xA3
#define TCS3410_CFG3_REG                            0xA4
#define TCS3410_CFG4_REG                            0xA5
#define TCS3410_CFG5_REG                            0xA6
#define TCS3410_CFG6_REG                            0xA7
#define TCS3410_CFG7_REG                            0xA8
#define TCS3410_CFG8_REG                            0xA9
#define TCS3410_CFG9_REG                            0xAA
#define TCS3410_AGC_NR_SAMPLES_L_REG                0xAC
#define TCS3410_AGC_NR_SAMPLES_H_REG                0xAD
#define TCS3410_TRIGGER_MODE_REG                    0xAE
#define TCS3410_CONTROL_REG                         0xB1
#define TCS3410_INTENAB_REG                         0xBA
#define TCS3410_SIEN_REG                            0xBB
#define TCS3410_MOD_COMP_CFG1_REG                   0xCE
#define TCS3410_MEAS_SEQR_FD_0_REG                  0xCF
#define TCS3410_MEAS_SEQR_ALS_FD_1_REG              0xD0
#define TCS3410_MEAS_SEQR_APERS_AND_VSYNC_WAIT_REG  0xD1
#define TCS3410_MEAS_SEQR_RESIDUAL_0_REG            0xD2
#define TCS3410_MEAS_SEQR_RESIDUAL_1_AND_WAIT_REG   0xD3
#define TCS3410_MEAS_SEQR_STEP0_MOD_GAINX_L_REG     0xD4
#define TCS3410_MEAS_SEQR_STEP0_MOD_GAINX_H_REG     0xD5
#define TCS3410_MEAS_SEQR_STEP1_MOD_GAINX_L_REG     0xD6
#define TCS3410_MEAS_SEQR_STEP1_MOD_GAINX_H_REG     0xD7
#define TCS3410_MEAS_SEQR_STEP2_MOD_GAINX_L_REG     0xD8
#define TCS3410_MEAS_SEQR_STEP2_MOD_GAINX_H_REG     0xD9
#define TCS3410_MEAS_SEQR_STEP3_MOD_GAINX_L_REG     0xDA
#define TCS3410_MEAS_SEQR_STEP3_MOD_GAINX_H_REG     0xDB
#define TCS3410_MEAS_SEQR_STEP0_MOD_PHDX_SMUX_L_REG 0xDC
#define TCS3410_MEAS_SEQR_STEP0_MOD_PHDX_SMUX_H_REG 0xDD
#define TCS3410_MEAS_SEQR_STEP1_MOD_PHDX_SMUX_L_REG 0xDE
#define TCS3410_MEAS_SEQR_STEP1_MOD_PHDX_SMUX_H_REG 0xDF
#define TCS3410_MEAS_SEQR_STEP2_MOD_PHDX_SMUX_L_REG 0xE0
#define TCS3410_MEAS_SEQR_STEP2_MOD_PHDX_SMUX_H_REG 0xE1
#define TCS3410_MEAS_SEQR_STEP3_MOD_PHDX_SMUX_L_REG 0xE2
#define TCS3410_MEAS_SEQR_STEP3_MOD_PHDX_SMUX_H_REG 0xE3

#define PRINT_DEBUG(fmt, args...) printf("Debug in %s at line %d: " fmt "", __FILE__, __LINE__, ##args)
#define MAX_RESULT_CONUT 15


typedef struct {
    float clear;
    int red;
    int green;
    int blue;
} als_result_t;

typedef enum {
    ALS_IDLE,
    ALS_START_MEASUREMENT,
    ALS_WAITING,
	ALS_DELAY_BEFORE_STEP0,
    ALS_READ_STEP0,
    ALS_DELAY_BEFORE_STEP1,
    ALS_READ_STEP1,
    ALS_PROCESS_RESULT,
	ALS_DATA_CALC
} als_state_t;

extern als_result_t result_list[MAX_RESULT_CONUT];

void tcs3410_init();
void read_als_data();
#endif